<template>
  <div class="TaskDispath">
    <div class="TaskDispath_top">
      <el-button type="primary" @click="goback">返回</el-button>
    </div>
    <div class="TaskDispath_left">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        :label-position="labelPosition"
      >
        <div class="ruleForm_left">
          <el-form-item>
            <h2>基本信息</h2>
          </el-form-item>
          <el-form-item label="任务编号" prop="workOrderNumber">
            <el-input
              v-model="ruleForm.workOrderNumber"
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="任务类型" prop="workOrderType">
            <span
              v-if="ruleForm.workOrderType == '0'"
              class="workOrder"
              style="padding-left: 15px"
              >警告</span
            >
            <span
              v-if="ruleForm.workOrderType == '1'"
              class="workOrder"
              style="padding-left: 15px"
              >报修</span
            >
            <span
              v-if="ruleForm.workOrderType == '2'"
              class="workOrder"
              style="padding-left: 15px"
              >保养</span
            >
          </el-form-item>
          <el-form-item label="提交时间" prop="add_time">
            <el-input
              v-model="ruleForm.add_time"
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="提交内容" prop="content">
            <el-input
              v-model="ruleForm.content"
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="提交图片" prop="name">
            <div class="task_pic">
              <viewer>
                <img
                  v-for="item in ruleForm.pic"
                  :src="item.pictureAddress"
                  :key="item.id"
                  alt
                  style="margin: 10px"
                />
              </viewer>
            </div>
          </el-form-item>
          <el-form-item label="提交视频" prop="name">
            <div class="task_pic">
              <viewer>
                <video
                  v-for="item in ruleForm.video"
                  controls="controls"
                  :src="item.videoAddress"
                  :key="item.id"
                  style="width: 100px; height: 100px; margin: 5px"
                ></video>
              </viewer>
            </div>
          </el-form-item>
        </div>
        <div class="ruleForm_right">
          <el-form-item prop="name">
            <h2>设备信息</h2>
          </el-form-item>
          <el-form-item label="设备编号" prop="deviceNumber">
            <el-input
              v-model="
                ruleForm.deviceNumber == null
                  ? '暂无数据'
                  : ruleForm.deviceNumber
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="设备名称" prop="name">
            <el-input
              v-model="ruleForm.name == null ? '暂无数据' : ruleForm.name"
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="设备类型" prop="typeName">
            <el-input
              v-model="
                ruleForm.typeName == null ? '暂无数据' : ruleForm.typeName
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="设备型号" prop="modelId">
            <el-input
              v-model="
                ruleForm.modelName == null ? '暂无数据' : ruleForm.modelName
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="设备位置" prop="name">
            <el-input
              v-model="
                ruleForm.storeAddress == null
                  ? '暂无数据'
                  : ruleForm.storeAddress
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="提交人" prop="addPerson">
            <el-input
              v-model="
                ruleForm.addPerson == null ? '暂无数据' : ruleForm.addPerson
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              v-model="
                ruleForm.addPersonPhone == null
                  ? '暂无数据'
                  : ruleForm.addPersonPhone
              "
              :readonly="taskRead"
            ></el-input>
          </el-form-item>
        </div>
        <div class="ruleForm_table" style="margin-top: 20px">
          <div class="ruleForm_table_top">
            <el-form-item prop="name">
              <h2>服务商委派</h2>
            </el-form-item>
            <el-form-item prop="name" style="margin-left: auto">
              <el-input
                placeholder="请输入服务商名称"
                v-model="serviceSearchQuery.name"
                class="input-with-select"
                clearable
                @change="getserviceSearch"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="getserviceSearch"
                ></el-button>
              </el-input>
            </el-form-item>
          </div>
          <el-table
            :data="serveList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            @current-change="chooseMcaterialChange"
            ref="Table"
          >
            <el-table-column
              type="selection"
              label="选择"
              width="55"
            ></el-table-column>
            <el-table-column label="服务商区域" width="250">
              <template slot-scope="scoped">
                <div>
                  {{ scoped.row.name_province }}{{ scoped.row.name_city
                  }}{{ scoped.row.name_area }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="服务商名称"></el-table-column>
            <el-table-column prop="stats" label="星级" sortable>
              <template slot-scope="scoped">
                <div>
                  <el-rate
                    v-model="scoped.row.stats"
                    disabled
                    text-color="#ff9900"
                  ></el-rate>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="distance" label="距离" sortable>
              <template slot-scope="scoped">
                <div>
                  {{ scoped.row.distance }}
                  <span style="color: red">km</span>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagination">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :page-sizes="[10, 20, 30, 40, 50]"
              :page-size="serviceSearchQuery.pageSize"
              :current-page="serviceSearchQuery.pageNumber"
              layout="total, sizes, prev, pager, next, jumper"
              :total="servetotal"
              class="management_page"
            ></el-pagination>
          </div>
        </div>
        <el-form-item style="margin-left: 20px">
          <el-button type="primary" @click="handlerSureTask"
            >确认派发</el-button
          >
          <!-- <el-button>重置</el-button> -->
          <el-button type="success" @click="showSuccess">已完成</el-button>
        </el-form-item>
      </el-form>

      <el-dialog
        title="备注信息"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose"
      >
        <el-form
          :model="successForm"
          :rules="successrules"
          ref="successref"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="备注信息" prop="remark">
            <el-input
              type="textarea"
              v-model="successForm.remark"
              :rows="3"
              style="width: 350px"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handlerSuccess">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  orderDetail,
  serviceSearch,
  orderSend,
  orderStatusChange
} from "../../utils/api";
import { mapState, mapGetters } from "vuex";
export default {
  data() {
    return {
      ruleForm: {
        workOrderNumber: "",
        workOrderType: "",
        add_time: "",
        path: "",
        deviceNumber: "",
        name: "",
        typeName: "",
        modelId: "",
        addPerson: "",
        deviceId: "",
        pic: [],
        video: []
      },
      taskRead: true,
      labelPosition: "top",
      // 工单id
      orderId: "",
      // 服务商id
      serviceId: "",
      content: "",
      // 服务列表的分页
      serviceSearchQuery: {
        pageNumber: 1,
        pageSize: 10,
        deviceId: "",
        name: ""
      },
      // 设备id
      DeviceId: "",
      // 服务商列表
      serveList: [],
      // 服务列表的总条数
      servetotal: 0,
      //登录的用户id
      userId: "",
      // 已完成的弹框
      successForm: {
        remark: ""
      },
      //已完成的显示隐藏
      dialogVisible: false,
      // 已完成的校验
      successrules: {
        remark: [{ required: true, message: "请输入备注信息", trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapState(["org_id"])
  },
  watch: {
    org_id(val) {
      this.getorderDetail();
    }
  },
  methods: {
    getDataInit() {
      if (this.org_id === "" || this.org_id === null || !this.org_id) {
        // this.LockersList = [];
        this.$message.error("暂无数据");
      } else {
        this.getorderDetail();
      }
    },
    // 返回上一级
    goback() {
      this.$store.dispatch("delVisitedViews", this.$route).then(obj => {
        // this.$router.push("/AllwokOrder");
        this.$router.go(-1);
      });
      window.sessionStorage.removeItem("orderId");
    },
    // 获取工单详情列表
    getorderDetail() {
      const params = {
        id: this.orderId
      };
      this.postRequest(orderDetail, params).then(res => {
        if (res.code == 200) {
          this.ruleForm = res.data[0];
          this.serviceSearchQuery.deviceId = res.data[0].deviceId;
          // 获取服务商列表
          this.getserviceSearch();
        }
      });
    },
    // 获取服务商列表
    getserviceSearch() {
      const params = {
        pageNumber: this.serviceSearchQuery.pageNumber,
        pageSize: this.serviceSearchQuery.pageSize,
        deviceId: this.serviceSearchQuery.deviceId,
        name: this.serviceSearchQuery.name,
        companyId: this.org_id
      };
      this.postRequest(serviceSearch, params).then(res => {
        // console.log(res);
        if (res.code == 200) {
          res.data.data.map(item => {
            item.stats = Number(item.stats);
          });
          this.serveList = res.data.data;
          this.servetotal = res.data.count;
        }
      });
    },
    // 点击表格中的选框
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.Table.clearSelection();
        this.$refs.Table.toggleRowSelection(val.pop());
      } else {
        if (val.length == 0) {
          this.serviceId = [];
        } else {
          this.serviceId = val[0].id;
        }
      }
    },
    chooseMcaterialChange(val) {
      this.$refs.Table.toggleRowSelection(val);
    },
    // 确认派发
    handlerSureTask() {
      const params = {
        id: this.orderId,
        serviceId: this.serviceId,
        userId: this.userId
      };
      // console.log(params);
      if (this.serviceId == "") {
        return this.$message.error("请选择派发的服务商");
      }
      this.postRequest(orderSend, params).then(res => {
        // console.log(res);
        if (res.code == 200) {
          this.$message.success("确认派单成功");
          this.$router.push("/AllwokOrder");
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.serviceSearchQuery.pageSize = val;
      this.getserviceSearch();
    },
    handleCurrentChange(val) {
      this.serviceSearchQuery.pageNumber = val;
      this.getserviceSearch();
    },
    // 点击完成触发的函数
    showSuccess() {
      this.dialogVisible = !this.dialogVisible;
    },
    // 点击确定更改完成
    handlerSuccess() {
      const params = {
        id: this.ruleForm.id,
        status: 4,
        userId: window.sessionStorage.getItem("userId"),
        deviceId: this.ruleForm.deviceId,
        remark: this.successForm.remark
      };
      this.$refs.successref.validate(valid => {
        if (!valid) return this.$message.error("请填写完整");
        this.postRequest(orderStatusChange, params).then(res => {
          if (res.code == 200) {
            this.$message.success("更改工单状态完成");
            this.dialogVisible = false;
            this.$router.push("/AllwokOrder");
          }
        });
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.successref.resetFields();
    }
  },
  created() {
    // 获取工单id
    this.orderId = window.sessionStorage.getItem("orderId");
    // 获取工单详情列表

    this.userId = window.sessionStorage.getItem("userId");
    this.getDataInit();
  }
};
</script>
<style lang="less">
.TaskDispath {
  .el-table__header-wrapper .el-checkbox {
    //找到表头那一行，然后把里面的复选框隐藏掉
    display: none;
  }
  .TaskDispath_left {
    border: 1px solid #eceff4;
    width: 96%;
  }
  .TaskDispath_top {
    width: 95%;
    padding: 10px;
    border: 1px solid #eceff4;
    background: #fff;
  }
  .demo-ruleForm {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    .ruleForm_left {
      margin-top: 20px;
      margin-left: 20px;
      width: 20%;
      .workOrder {
        width: 345.97px;
        height: 40px;
        border: 1px solid #dcdfe6;
        display: block;
        border-radius: 5px;
      }
      .task_pic {
        width: 345px;
        height: 120px;
        border: 1px solid #dcdfe6;
        img {
          width: 80px;
          height: 80px;
        }
      }
    }
  }
  @media screen and(max-width:1366px) {
    .task_pic {
      width: 238px !important;
      height: 100px;
      border: 1px solid #dcdfe6;
    }
    .workOrder {
      width: 238px !important ;
    }
  }
  .ruleForm_right {
    margin-top: 20px;
    width: 20%;
  }
  .ruleForm_table {
    width: 50%;
    .ruleForm_table_top {
      display: flex;
      flex-direction: row;
    }
  }
}
</style>
